/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.quickorm.sample;

import com.quickorm.core.QuickormTemplate;
import com.quickorm.sample.entity.User;
import com.quickorm.core.support.QuickormDaoSupport;
import com.quickorm.utils.PagedList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author aaa
 */
public class Menu3 {

    public static void execute(int dataRowCount) {
        Program.userDao.deleteUserByLoginName("testuser");
        JdbcBatchInsert(dataRowCount);
        Program.userDao.deleteUserByLoginName("testuser");
        QuickOrmBatchInsert(dataRowCount);
        Program.userDao.deleteUserByLoginName("testuser");
    }

    public static void QuickOrmBatchInsert(int dataRowCount) {
        List<User> list = new ArrayList<User>(dataRowCount);
        for (int i = 0; i <= dataRowCount - 1; i++) {
            User testUser = new User();
            testUser.setUserId(Integer.toString(i + 1000));
            testUser.setName("TestUser");
            testUser.setLoginName("testuser");
            testUser.setPassword("xxxxxxxxxxxx");
            list.add(testUser);
        }
        long beginTime = System.currentTimeMillis();
        Program.userDao.saveBatch(list);
        long endTime = System.currentTimeMillis();

        System.out.println(String.format("QuickOrm BATCH INSERT %s rows data used time:%s ms", dataRowCount, (endTime - beginTime)));


        QuickormTemplate quickormTemplate = ((QuickormDaoSupport) Program.userDao).getQuickormTemplate();
        beginTime = System.currentTimeMillis();
        List listt1 = quickormTemplate.getList(User.class, null, 1, 10);
        endTime = System.currentTimeMillis();
        System.out.println(String.format("SELECT BEGIN Page 1 used time:%s ms", (endTime - beginTime)));

        beginTime = System.currentTimeMillis();
        PagedList listt2 = quickormTemplate.getList(User.class, null, 50000, 10);
        endTime = System.currentTimeMillis();
        System.out.println(String.format("SELECT BEGIN Page 50000 used time:%s ms", (endTime - beginTime)));

        beginTime = System.currentTimeMillis();
        PagedList listt3 = quickormTemplate.getList(User.class, null, 100000, 10);
        endTime = System.currentTimeMillis();
        System.out.println(String.format("SELECT BEGIN Page 100000 used time:%s ms", (endTime - beginTime)));
    }

    public static void JdbcBatchInsert(int dataRowCount) {
        long beginTime = System.currentTimeMillis();
        try {
            Connection conn = Program.dataSource.getConnection();
            conn.setAutoCommit(false);

            PreparedStatement stat = conn.prepareStatement("INSERT INTO User(UserId, Name, LoginName, Password) VALUES (?, ?, ?, ?)");
            for (int i = 0; i <= dataRowCount - 1; i++) {
                stat.setObject(1, i + 1000);
                stat.setObject(2, "TestUser");
                stat.setObject(3, "testuser");
                stat.setObject(4, "xxxxxxxxxxxx");
                stat.addBatch();

                if (i % 1000 == 0) {
                    stat.executeBatch();
                    stat.clearBatch();
                }
            }
            stat.executeBatch();
            conn.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        long endTime = System.currentTimeMillis();

        System.out.println(String.format("JDBC BATCH INSERT %s rows data used time:%s ms", dataRowCount, (endTime - beginTime)));
    }
}
